﻿
using MySql.Data.MySqlClient;
using newBlog.CenterInc.model;
using newBlog.inc.SqlINC;
using System.Collections.Generic;

namespace newBlog.CenterInc.Ctrltor
{
    public class Comment_inc
    {
        sqlConnect Conn = new sqlConnect();
        public bool GetComment(string user_id, string article_id, string comtent)
        {
            string sql = "insert into blog_comment(Blog_user_id,Blog_article_id,Blog_comment_content) values('{0}','{1}','{2}')";
            if (Conn.AddDelUp(string.Format(sql, user_id, article_id, comtent)) < 1)
            {
                return true;
            }
            return false;
        }//发表评论
        public List<Comment_ArticleDao> Comment_Article_items(int article_id)
        {
            List<Comment_ArticleDao> comment_ArticleDaos = new List<Comment_ArticleDao>();
            string sql = "SELECT" +
                " users.Blog_user_portrait," +
                "users.Blog_user_name," +
                "comments.Blog_comment_content " +
                "FROM " +
                "blog_comment AS comments " +
                "LEFT JOIN blog_user AS users ON comments.Blog_user_id = users.Blog_user_id " +
                "WHERE " +
                "comments.Blog_article_id = '{0}' " +
                "AND " +
                "comments.Blog_comment_state = 1 " +
                "ORDER BY " +
                "comments.Blog_comment_id DESC " +
                "LIMIT 0,20";
            MySqlDataReader dr = Conn.DataListAll(string.Format(sql, article_id));
            while (dr.Read())
            {
                Comment_ArticleDao comment_ArticleDao = new Comment_ArticleDao(dr.GetString(0), dr.GetString(1), dr.GetString(2));
                comment_ArticleDaos.Add(comment_ArticleDao);
            }
            return comment_ArticleDaos;
        }//获取评论到文章

        public int MyComment_Count(string user_id, string minTime,string maxTime,string content,string article_id) {
            string sql = $"SELECT count(Blog_comment_id) FROM blog_comment where Blog_comment_content like '%{content}%'";
            if (user_id.Length!=0) {
                sql += $" and Blog_user_id='{user_id}'";
            }
            if (minTime.Length!=0) {
                sql += $" and Blog_comment_date>='{minTime}'";
            }
            if (maxTime.Length!=0) {
                sql += $"  and Blog_comment_date<='{maxTime}'";
            }
            if (article_id.Length!=0) {
                sql += $" and Blog_article_id='{article_id}'";
            }
            return Conn.DataValue(sql);
        }//通过统计评论的数量

        public List<Comment_ArticleDao> MyComment_items(string user_id, string minTime, string maxTime, string content, string article_id) {
            List<Comment_ArticleDao> list = new List<Comment_ArticleDao>();
            string sql = $"SELECT Blog_comment_content,Blog_comment_id,Blog_user_id,Blog_article_id,Blog_comment_date FROM blog_comment where Blog_comment_content like '%{content}%'";
            if (user_id.Length != 0)
            {
                sql += $" and Blog_user_id='{user_id}'";
            }
            if (minTime.Length != 0)
            {
                sql += $" and Blog_comment_date>='{minTime}'";
            }
            if (maxTime.Length != 0)
            {
                sql += $"  and Blog_comment_date<='{maxTime}'";
            }
            if (article_id.Length != 0)
            {
                sql += $" and Blog_article_id='{article_id}'";
            }
            MySqlDataReader dr = Conn.DataListAll(sql);
            while (dr.Read()) {
                Comment_ArticleDao comment_ArticleDao = new Comment_ArticleDao(dr.GetString(0),dr.GetString(1),dr.GetString(2),dr.GetString(3),dr.GetString(4));
                list.Add(comment_ArticleDao);
            }
            return list;
        }//所有评论内容

        public bool delComment(string comment_id) {
            string sql = "delete from blog_comment where Blog_comment_id='{0}'";
            if (Conn.AddDelUp(string.Format(sql,comment_id))<1) {
                return true;
            }
            return false;
            }
    }
}